<?php

class Controller_Site extends Controller_Action
{

	CONST MAIN_PAGE_ACTIVE_USERS_LIMIT = 6;

	/**
	 * Community Discussion page. limit for topics per page
	 */
	CONST FULL_TOPIC_LIST_LIMIT = 10;

	const COMMUNITY_LIST_LIMIT = 6;

	const STARHOUSE_LIST_LIMIT = 6;
	const STARHOUSE_LIST_LIMIT_GET = 50;

	const PHOTO_CATALOG_LIMIT = 12;
	
	
	protected $cacheService;
	protected $tagService;
	protected $newsService;
	protected $blogService;
	protected $profileService;
	
	
	/**
	 * @return ProfileService
	 */
	public function getProfileService() {
		if (!$this->profileService) {
			$this->profileService = ProfileService::getInstance();
		}
		return $this->profileService;
	}
	
	/**
	 * @return BlogService
	 */
	public function getBlogService() {
		if (!$this->blogService) {
			$this->blogService = BlogService::getInstance();
		}
		return $this->blogService;
	}

	
	/**
	 *
	 * @var Db_Adapter
	 */
	protected $adapter;
	
	/**
	 * @return Db_Adapter
	 */
	public function getAdapter() {
		return $this->adapter;
	}
	/**
	 * Getter for CacheService
	 *
	 * @return CacheService
	 */
	protected function getCacheService()
	{
		if (!$this->cacheService) {
			$this->cacheService = CacheService::getInstance();
		}
		return $this->cacheService;
	}	
	
	/**
	 * @return NewsService
	 */
	public function getNewsService() {
		if (!$this->newsService) {
			$this->newsService = NewsService::getInstance();
		}		
		return $this->newsService;
	}
	
	/**
	 * @return TagService
	 */
	public function getTagService() {
		if (!$this->tagService) {
			$this->tagService = TagService::getInstance();
		}
		return $this->tagService;
	}

	
	/**
	 * __construct for Controller
	 * @return void
	 */
	public function init()
	{
		parent::init();
		$this->adapter = Db::getConnection();
		$this->_initMenu();
		

		$this->view->contentlist = array();
		$this->view->css = array();
		$this->view->js = array();
		
		$this->_initCurrentUser();
				
//		$this->view->exclusive = $this->_getExclusive();
	}

	/**
	 * Set to view menu elements with isCurrent flags.
	 *
	 * @return void
	 */
	protected function _initMenu()
	{
		$indexUrl = u(array('controller' => 'index', 'action' => 'index'), 'default', true);
		$consultantUrl = u(array('controller' => 'consultants', 'action' => 'index'), 'default', true);
		$serviceUrl = u(array('controller' => 'service', 'action' => 'index'), 'default', true);
		$shopUrl = u(array('controller' => 'shop', 'action' => 'index'), 'default', true);
		$clubUrl = u(array('controller' => 'games', 'action' => 'index'), 'default', true);
		$offerUrl = u(array('controller' => 'offer', 'action' => 'index'), 'default', true);

		$current = $this->_getCurrentMenuSection();
		$this->view->menuCurrent = $current;
		$this->view->menu = array(
			array('title' => 'Главная', 'url' => $indexUrl, 'isCurrent' => ($current == 'default' ? 1 : 0)),
			array('title' => 'Консультанты', 'url' => $consultantUrl, 'isCurrent' => ($current == 'consultants' ? 1 : 0)),
			array('title' => 'Услуги', 'url' => $serviceUrl, 'isCurrent' => ($current == 'service' ? 1 : 0)),
			array('title' => 'Магазин', 'url' => $shopUrl, 'isCurrent' => ($current == 'shop' ? 1 : 0)),
			array('title' => 'Клуб Колизео', 'url' => $clubUrl, 'isCurrent' => ($current == 'club' ? 1 : 0)),
			array('title' => 'Акции и скидки', 'url' => $offerUrl, 'isCurrent' => ($current == 'offer' ? 1 : 0)),
		);
		
	}


	/**
	 * Trying to use Zend_Navigation as
	 */
	protected function _initMenuByZendNavigation()
	{
		$path = BASE_DIR . '/config/sitemap.xml';
		//$frontend = Cache_Frontend_Config::getInstance();

		// Cache wrap for loading xml navigation file
		//$sitemap = $frontend->load('sitemap');
		//if (!$sitemap) {
			$sitemap = Config_Xml_Factory::factory('sitemap', true);
			//$sitemap = new Zend_Config_Xml($path);
		//}

		$container = new Navigation($sitemap);
		$this->_container = $container;

		$navigatorHelper = new Zend_View_Helper_Navigation($container);
		$navigatorHelper->setView(new Zend_View());

		$crumbs = $navigatorHelper->breadcrumbs($container);
		$crumbs->setLinkLast(0);
		$crumbs->setMinDepth(0);
		$crumbs->setSeparator(' / ');

		$crumbs = $crumbs->render();
		$this->view->breadcrumbs = $crumbs;

		$one = $container->findOneBy('controller', $this->getRequest()->getControllerName());
		$title = '';
		if ($one) {
			$title = $one->get('label');
		}
		$this->view->title = $title;
	}

	/**
	 * Return key of current changed menu section.
	 *
	 * @return string
	 */
	protected function _getCurrentMenuSection()
	{
		return 'default';
	}

	/**
	 * Adding contentlist to view instant.
	 * Contentlist getting by key.
	 *
	 * @param string $key key from table contentlist
	 * @return void
	 */
	protected function _addContentListByKey($key, $name = NULL)
	{
		$peer = new ContentlistPeer();
		if (!$key) {
			$key = "test";
		}

		$contentlist = $peer->getContentListByKey($key);
		if (!$name){$name = $key;}
		if($contentlist)
			$this->_addContentList($name, $contentlist);
	}

	/**
	 * Adding contentlist to view.
	 *
	 * @param string $key Array index.
	 * @param ContentList $contentList
	 * @return void
	 * @throws Exception
	 */
	protected function _addContentList($key, $contentList)
	{
		if (!$contentList instanceof ContentList) {
			throw new Exception('ContentList is not valid');
		}

		$lists = $this->view->contentlist;
		if (isset($lists[$key])) {
			throw new Exception("ContentList with key `$key` already exists");
		}

		$lists[$key] = $contentList;
		$this->view->contentlist = $lists;
	}
	
	protected function _addContentListByKey2($key, $params = array(), $name = NULL)
	{
		$peer = new ContentlistPeer();
		if (!$key) {
			$key = "test";
		}

		$contentlist = $peer->getContentListByKey2($key, $params);
		if (!$name){$name = $key;}
		if($contentlist) {
			$this->_addContentList($name, $contentlist);
		}
	}
	
	
	/**
	 * Create paginator object by count elements.
	 *
	 * @param integer $count
	 * @return array Paginator::getArray()
	 */
	protected function _getPaginator($count, $limit = 10)
	{
		$page = $this->_getParam('page');
		$params = array(
			'currentPage' => intval($page),
			'countItemAll' => intval($count),
			'countPageForShow' => 5,
			'countItemForShow' => $limit
		);
		$paginator = new Paginator($params);
		$array = $paginator->getArray();
		return $array;
	}


	
	private function _initCurrentUser(){
		$profile = Auth::getInstance()->getProfile();
		if($profile){
			$this->view->currentUser = $profile->getInfo();
		}
	}


}
